@{var tocModel = Model.GetToc();}
@if (!(tocModel?.Node is null))
{
    var chosenParent = (tocModel.Node.Children?.Any() ?? false)
        ? tocModel.Node
        : tocModel.Node?.Parent;
    if (!(chosenParent is null))
    {
        <div class="col-12 col-md-3 col-xl-2 ct-sidebar">
            <nav class="collapse ct-links" id="ct-docs-nav">
                <div class="ct-toc-item active">
                    @if ((chosenParent.Parent?.Children?.Any(x => x != chosenParent) ?? false) && !(chosenParent.Parent?.Parent is null))
                    {
                        // our parent has other children and our parent is not the top-level navigation
                        <div class="dropdown">
                            <a class="dropdown-toggle ct-toc-link" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                @chosenParent.Name
                            </a>
                            <div class="dropdown-menu silk-dropdown-menu" aria-labelledby="dropdownMenuButton">
                                <a class="dropdown-item" href="@InputUrl(chosenParent.Parent.Href)"><i class="ni ni-bold-up"></i> @chosenParent.Parent.Name</a>
                                @foreach (var sibling in chosenParent.Parent.Children)
                                {
                                    @if (sibling != chosenParent)
                                    {
                                        <a class="dropdown-item" href="@InputUrl(sibling.Href)"><i class="ni ni-bold-right"></i> @sibling.Name</a>
                                    }
                                }
                            </div>
                        </div>
                    }
                    else if (!(chosenParent is null))
                    {
                        <a class="ct-toc-link" href="@InputUrl(chosenParent.Href)">
                            @chosenParent.Name
                        </a>
                    }
                    <ul class="nav ct-sidenav">
                        @foreach (var child in chosenParent?.Children ?? Enumerable.Empty<TableOfContentsElement>())
                        {
                            // ct-docs-nav
                            @if (child == tocModel.Node)
                            {
                                <li class="active ct-sidenav-active">
                                    <a href="@InputUrl(child.Href)">
                                        @child.Name
                                    </a>
                                </li>
                            }
                            else
                            {
                                <li>
                                    <a href="@InputUrl(child.Href)">
                                        @child.Name
                                    </a>
                                </li>
                            }
                        }
                    </ul>
                </div>
            </nav>
        </div>
    }
}